<?php

namespace App\Containers\AppSection\Moderation\Tasks;

use App\Containers\AppSection\Moderation\Data\Repositories\ModerationRepository;
use App\Ship\Criterias\Where;
use App\Ship\Parents\Tasks\Task;
use App\Ship\Traits\FilterByDateTrait;
use Carbon\Carbon;

class GetAllModerationsTask extends Task
{
    use FilterByDateTrait;

    protected ModerationRepository $repository;

    public function __construct(ModerationRepository $repository)
    {
        $this->repository = $repository;
    }

    public function run()
    {
        return $this->repository->paginate();
    }

    public function withStage($stage): self
    {
        $this->repository->pushCriteria(new Where('stage', '=', $stage));

        return $this;
    }

    public function active(): self
    {
        $this->repository->has('projects.moderation');

        return $this;
    }

    public function withProjectStatus($status): self
    {
        $this->repository->whereHas('project', function ($query) use ($status) {
            $query->where('moderation_status_key', $status);
        });

        return $this;
    }

    public function forProject($projectId): self
    {
        $this->repository->pushCriteria(new Where('project_id', '=', $projectId));

        return $this;
    }

    public function whereCondition($column, $from, $to): self
    {
        if (!empty($from)) {
            $this->repository->whereHas('project.condition', function ($query) use ($column, $from) {
                $query->where($column, '>=', $from);
            });
        }

        if (!empty($to)) {
            $this->repository->whereHas('project.condition', function ($query) use ($column, $to) {
                $query->where($column, '<=', $to);
            });
        }

        return $this;
    }

    public function filterByProjectDate($from, $to): self
    {
        if (!empty($from)) {
            $this->repository->whereHas('project', function ($query) use ($from) {
                $query->where('created_at', '>=', Carbon::parse($from));
            });
        }

        if (!empty($to)) {
            $this->repository->whereHas('project', function ($query) use ($to) {
                $query->where('created_at', '<=', Carbon::parse($to));
            });
        }

        return $this;
    }

    public function withPledge($pledge): self
    {
        if ($pledge == 1) {
            $this->repository->has('project.pledges');
        }

        if ($pledge == 2) {
            $this->repository->haveCount('project.pledges', 0);
        }

        return $this;
    }

    public function withType($type): self
    {
        if ($type == 1) {
            $this->repository->haveCount('project.moderations', 1);
        }

        if ($type == 2) {
            $this->repository->haveCount('project.moderations', 1, '>');
        }

        return $this;
    }

    public function daysOfReturned($from, $to): self
    {
        if (!empty($from)) {
            $this->repository->pushCriteria(
                new Where('updated_at', '>=', Carbon::parse($from))
            );
        }

        if (!empty($to)) {
            $this->repository->pushCriteria(
                new Where('updated_at', '<=', Carbon::parse($to))
            );
        }

        return $this;
    }
}
